package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdUserMapper;
import com.heima.admin.service.AdUserService;
import com.heima.common.exception.CreateProjectException;
import com.heima.model.admin.dtos.AdUserDto;
import com.heima.model.admin.pojos.AdUser;
import com.heima.model.common.constants.AdminConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.AppJwtUtil;
import com.heima.utils.common.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

/**
 * <p>
 * 管理员用户信息表 服务实现类
 * </p>
 *
 * @author tps
 * @since 2021-01-09
 */
@Service
public class AdUserServiceImpl extends ServiceImpl<AdUserMapper, AdUser> implements AdUserService {
    @Autowired
    AdUserMapper adUserMapper;


    @Override
    public ResponseResult login(AdUserDto adUserDto) {
        //检验参数
        if(EmptyUtil.isNullOrEmpty(adUserDto)) {
            CreateProjectException.exec(AppHttpCodeEnum.PARAM_INVALID);
        }
        //执行业务 查询
        AdUser adUser = getOne(Wrappers.<AdUser>lambdaQuery().eq(AdUser::getName, adUserDto.getName())
                .eq(AdUser::getStatus, AdminConstants.USER_STATUS_ON));
        if(EmptyUtil.isNullOrEmpty(adUser)){
            CreateProjectException.exec(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        String dbstatus = String.valueOf(adUser.getStatus()) ;
        //判断用户状态是否可用
        if(!dbstatus.equals(AdminConstants.USER_STATUS_ON)){
            CreateProjectException.exec(AppHttpCodeEnum.USERNAME_IS_NOTUSE);
        }
        //判断密码
        String dbPassword = adUser.getPassword();
        String salt = adUser.getSalt();
        String pwd = DigestUtils.md5DigestAsHex((adUserDto.getPassword()+salt).getBytes());
        if(!pwd.equals(dbPassword)){
            CreateProjectException.exec(AppHttpCodeEnum.USERNAME_OR_PASSWORD_ERROR);
        }
        //重置上次登录时间
        adUser.setLoginTime(new Date());
        updateById(adUser);
        //返回给用户token 以及除了密码的用户数据
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("token",AppJwtUtil.getToken(adUser.getId().longValue()));
        adUser.setPassword(null);
        adUser.setSalt(null);
        resultMap.put("user",adUser);

        return ResponseResult.okResult(resultMap);
    }

    @Override
    public ResponseResult regist(AdUser adUser) {
        if(EmptyUtil.isNullOrEmpty(adUser)){
            CreateProjectException.exec(AppHttpCodeEnum.PARAM_INVALID);
        }
        String salt = UUID.randomUUID().toString().replaceAll("-", "");
        String password = DigestUtils.md5DigestAsHex((adUser.getPassword()+salt).getBytes());
        Date date = new Date();
        adUser.setPassword(password);
        adUser.setSalt(salt);
        adUser.setCreatedTime(date);
        adUser.setStatus(Integer.valueOf(AdminConstants.USER_STATUS_TEMP_CLOSE));

        save(adUser);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}

